package com.seventh.restaurant.web;

import com.seventh.restaurant.domain.Customer;
import com.seventh.restaurant.service.CustomerService;
import com.seventh.restaurant.web.util.HeaderUtil;

import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.hibernate.id.IdentifierGenerator.ENTITY_NAME;

@RestController
@RequestMapping("/api")
public class CustomerResource {

    private final CustomerService customerService;

    public CustomerResource(CustomerService customerService) {
        this.customerService = customerService;
    }

    @ApiOperation(value="新增顾客信息")
    @PostMapping("/customer")
    public ResponseEntity<Customer> createCustomer(@RequestParam String name,@RequestParam String phonenumber) throws Exception {
        if (name!=null && phonenumber!=null){
          Customer customer = new Customer();
          customer.setName(name);
          customer.setPhoneNumber(phonenumber);
          Customer customer1 = customerService.insertNewCustomer(customer);
            return ResponseEntity.ok().body(customer1);
        }else {
            return new ResponseEntity<Customer>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="删除顾客信息")
    @DeleteMapping("/customer")
    public ResponseEntity<Customer> deleteCustomer(@RequestParam Long id) throws Exception {
        if (id!=null){
            customerService.deleteCustomer(id);
            return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("Customer", id.toString())).build();
        }else {
            return new ResponseEntity<Customer>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="修改顾客信息")
    @PutMapping("/customer")
    public ResponseEntity<Customer> updateCustomer(@RequestParam String name,@RequestParam String phonenumber) throws Exception {
        if (name!=null && phonenumber!=null){
          Customer customer = new Customer();
          customer.setName(name);
          customer.setPhoneNumber(phonenumber);
           Customer customer1 = customerService.updateCustomer(customer);
            return ResponseEntity.ok()
                    .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, customer1.getId().toString()))
                    .body(customer1);
        }else {
            return new ResponseEntity<Customer>(HttpStatus.BAD_REQUEST);
        }
    }

    @ApiOperation(value="获得顾客信息List")
    @GetMapping("/customer/list")
    public ResponseEntity<List<Customer>> getAllCustomers() throws Exception {
        List<Customer> customers = customerService.findAllCustomers();
        return new ResponseEntity<>(customers, HttpStatus.OK);
    }

    @ApiOperation(value="根据手机号获得顾客信息")
    @GetMapping("/customer/one")
    public ResponseEntity<Customer> getOneCustomer(@RequestParam String phonenumber) throws Exception {
        if (phonenumber!=null){
            Customer customer = customerService.findCustomer(phonenumber);
            return new ResponseEntity<>(customer, HttpStatus.OK);
        }else {
            return new ResponseEntity<Customer>(HttpStatus.BAD_REQUEST);
        }
    }


}
